package mil.cdc.windows;

import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import mil.cdc.listeners.CustomKeyListener;
import mil.cdc.listeners.CustomMouseListener;
import mil.cdc.utils.CDCConstants;
import mil.cdc.utils.Question;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class ReviewWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea reviewArea;
	private JScrollPane reviewPane;
	private JButton closeBtn;

	public ReviewWindow() {
		initComponents();
	}

	private void initComponents() {
		setTitle("Review");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/mil/cdc/data/ico/af.gif")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(getReviewPane(), new Constraints(new Bilateral(13, 12, 22), new Leading(11, 519, 10, 10)));
		add(getCloseBtn(), new Constraints(new Bilateral(260, 259, 66), new Leading(536, 12, 12)));
		setSize(600, 600);
		setVisible(true);
	}

	private JButton getCloseBtn() {
		if (closeBtn == null) {
			closeBtn = new JButton();
			closeBtn.setText("Close");
			closeBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CLOSE));
		}
		return closeBtn;
	}

	private JScrollPane getReviewPane() {
		if (reviewPane == null) {
			reviewPane = new JScrollPane();
			reviewPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			reviewPane.setViewportView(getReviewArea());
		}
		return reviewPane;
	}

	private JTextArea getReviewArea() {
		if (reviewArea == null) {
			reviewArea = new JTextArea();
			reviewArea.setEditable(false);
			reviewArea.setLineWrap(true);
			reviewArea.setWrapStyleWord(true);
			reviewArea.addKeyListener(new CustomKeyListener(this));
		}
		return reviewArea;
	}
	
	/**
	 * Loads the questions into the window to display.
	 * @param questions Questions to display.
	 */
	public void loadReviewQuestion(ArrayList<Question> questions){
		for (Question q : questions) {
			int counter = 1;
			reviewArea.append(q.getQuestion() + "\n");
			for (String s : q.getChioces()) {
				reviewArea.append(String.format("\t%d)   %s\n", counter++, s));
			}
			reviewArea.append(String.format("Correct Answer is:  %d\n", q.getAnswer()));
			reviewArea.append(String.format("        You Selected:  %d\n\n", q.getSelectedAnswer()));
		}
	}
}
